﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Youz.Common.Helper;
using Youz.Common.Entity;

namespace Youz.Common.BLL
{
    public enum EmailTemplate
    {
        AccountPwd,   //发送帐号密码,注册
        ForgotPwd,       //重置登录密码
        SendCode,  //发送验证码 修改密码
        ActiveAccount,      //激活賬號
        ResetPassword,        //重置密碼
        VerificationEmail,//密保邮箱验证（用于更换密保邮箱 ）
        ReplaceEmail,//发送验证码 更换密保邮箱
        AdminResetPwd       //管理员重置密碼
    }

    public class EmailBLL
    {

        /// <summary>
        /// 发送重置密碼邮件
        /// </summary>
        /// <param name="username">FirstName + LastName</param>
        /// <param name="email">帐号(Email)</param>
        /// <param name="timeTicks">时间戳</param>
        /// <returns>空值则发送成功</returns>
        public static string SendResetPasswordEmail(string username, string email, string timeTicks)
        {
            string _ResetPwdURL = UrlHelper.AbsoluteWebRoot.AbsoluteUri + "ResetPassword/ResetPwdNew.aspx?sid=" + DESEncrypt.Encrypt(username) + "&d=" + DESEncrypt.Encrypt(timeTicks);

            string emailBody = GetMailTemplate(EmailTemplate.ResetPassword, Utility.CurrentLanguage);

            emailBody = emailBody.Replace("{@MemberAccount}", username).Replace("{@ValidateUrl}", _ResetPwdURL);

            return SendMail(email, Lang_LanguageBLL.LanguageByManage(LanguageModule.M_Common, "EmailSubject_ResetPwdURL", "重置密码 - 会员管理系统"), emailBody);
        }
        /// <summary>
        /// 发送验证码 修改密码
        /// </summary>
        /// <param name="username">FirstName + LastName</param>
        /// <param name="email">帐号(Email)</param>
        /// <param name="timeTicks">时间戳</param>
        /// <returns>空值则发送成功</returns>
        public static string SendCode(string username, string email, string code)
        {
            string emailBody = GetMailTemplate(EmailTemplate.SendCode, Utility.CurrentLanguage);

            emailBody = emailBody.Replace("{@MemberAccount}", username).Replace("{@Code}", code);

            return SendMail(email, Lang_LanguageBLL.LanguageByManage(LanguageModule.M_Common, "EmailSubject_SendCodeUpdatePwd", "密码修改"), emailBody);
        }
        /// <summary>
        /// 发送验证码 验证密保邮箱
        /// </summary>
        /// <param name="username">FirstName + LastName</param>
        /// <param name="email">帐号(Email)</param>
        /// <param name="timeTicks">时间戳</param>
        /// <returns>空值则发送成功</returns>
        public static string VerificationEmail(string username, string email, string code)
        {
            string emailBody = GetMailTemplate(EmailTemplate.VerificationEmail, Utility.CurrentLanguage);

            emailBody = emailBody.Replace("{@MemberAccount}", username).Replace("{@Code}", code);

            return SendMail(email, Lang_LanguageBLL.LanguageByManage(LanguageModule.M_Common, "VerificationEmail", "密保邮箱验证"), emailBody);
        }
        /// <summary>
        /// 发送验证码 更换密保邮箱
        /// </summary>
        /// <param name="username">FirstName + LastName</param>
        /// <param name="email">帐号(Email)</param>
        /// <param name="timeTicks">时间戳</param>
        /// <returns>空值则发送成功</returns>
        public static string ReplaceEmail(string username, string email, string code)
        {
            string emailBody = GetMailTemplate(EmailTemplate.ReplaceEmail, Utility.CurrentLanguage);

            emailBody = emailBody.Replace("{@MemberAccount}", username).Replace("{@Code}", code);

            return SendMail(email, Lang_LanguageBLL.LanguageByManage(LanguageModule.M_Common, "ReplaceEmail", "密保邮箱验证"), emailBody);
        }
        /// <summary>
        /// 调用接口获取邮件模板
        /// </summary>
        /// <param name="templateName"></param>
        /// <param name="langCode"></param>
        /// <returns></returns>
        public static string GetMailTemplate(EmailTemplate templateName, string langCode)
        {
            string content = CacheHelper.GetCache(templateName + langCode) as string;
            if (string.IsNullOrEmpty(content))
            {
                string postData = string.Format("templateName={0}&langCode={1}", templateName, langCode);

                string filePath = string.Format("/EmailTemplate/{0}/{1}.html", templateName, langCode);
                filePath = HttpContext.Current.Server.MapPath(filePath);
                string fileContent = string.Empty;
                using (var reader = new StreamReader(filePath))
                {
                    fileContent = reader.ReadToEnd();
                }
                content = fileContent;

                CacheHelper.SetCache(templateName + langCode, fileContent);
            }
            return content;
        }


        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="EmailAddress"></param>
        /// <param name="Subject"></param>
        /// <param name="strBody"></param>
        /// <returns></returns>
        public static string SendMail(string EmailAddress, string Subject, string strBody)
        {
            string strResult = EmailHelper.SendMail(EmailAddress, Subject, strBody.Replace("{@AdminCount}", ConfigParameters.SMTPServerUser), ConfigParameters.SMTPServer,
                    ConfigParameters.SMTPServerUser, ConfigParameters.SMTPServerPassword, Convert.ToInt32(ConfigParameters.SMTPPort));

            if (!string.IsNullOrEmpty(strResult))
            {
                Utility.WriteErrorLog(strResult, EmailAddress, strResult);
            }

            return strResult;

        }
    }
}
